package com.test.cordova.base;

import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.LifecycleRegistry;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.test.cordova.R;
import com.test.cordova.util.StatusBarUtil;

import butterknife.ButterKnife;

/**
 *通用带标题 返回键的基类
 *
 */

public abstract class BaseTitleActivity  extends FragmentActivity implements LifecycleOwner{

    protected LifecycleRegistry mLifecycleRegistry;

    public FrameLayout base_title_content;
    public ImageView common_navi_back;
    public TextView common_navi_title;
    protected LayoutInflater inflater;

    @Override
    public Lifecycle getLifecycle() {
        return mLifecycleRegistry;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        inflater = LayoutInflater.from(this);
        mLifecycleRegistry = new LifecycleRegistry(this);
        StatusBarUtil.setStatusBarMode(this, false, R.color.deepskyblue);

        setContentView(R.layout.base_title_layout);
        initView();
        setBaseContentView(getLayout());
        ButterKnife.bind(this);
        setTitle(addTitle());
        setListener();
    }

    private void initView(){
        base_title_content = findViewById(R.id.base_title_content);
        common_navi_title = findViewById(R.id.common_navi_title);
        common_navi_back = findViewById(R.id.common_navi_back);
        if (isShowBack()) {
            common_navi_back.setVisibility(View.VISIBLE);
            common_navi_back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
    }

    protected void setBackImageResource(int resid){
        common_navi_back.setImageResource(resid);
    }

    protected void setBackImageVisibility(int visibility){
        common_navi_back.setVisibility(visibility);
    }
    protected void setTitle(@NonNull String title){
        common_navi_title.setText(title);
    }

    protected void setBaseContentView(@NonNull int layoutid){
        View inflate = inflater.inflate(layoutid, null, false);
        base_title_content.addView(inflate);
    }

    protected void setBaseContentView(@NonNull View view){
        base_title_content.addView(view);
    }

    protected abstract void setListener();

    protected abstract int getLayout();

    protected abstract String addTitle();

    protected abstract boolean isShowBack();


}
